$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/franchisee/list',
        datatype: "json",
        colModel: [			
			{ label: '用户ID', name: 'userId', index: "user_id", width: 45, key: true },
			{ label: '用户名', name: 'username', width: 75 },
			{ label: '邮箱', name: 'email', width: 90 },
			{ label: '手机号', name: 'mobile', width: 100 },
			{ label: '状态', name: 'status', width: 60, formatter: function(value, options, row){
				return value === 0 ? 
					'<span class="label label-danger">禁用</span>' : 
					'<span class="label label-success">正常</span>';
			}},
			{ label: '创建时间', name: 'createTime', index: "create_time", width: 85},
            { label: '操作', name: 'sy', width: 100,align:'center',formatter: function(cellvalue, options, rowdata) {

                    return "<a style='cursor:pointer;' onclick='vm.update("+ rowdata.userId +")'>" +
                        "修改</a>&nbsp;&nbsp;&nbsp;&nbsp;" +
                        "<a style='cursor:pointer;' onclick='vm.del("+ rowdata.userId +")'>"+
                        "删除</a>";


                }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el:'#rrapp',
    data:{
        fileList1: [],
        fileList2: [],
        q:{
            username: null
        },
        value: '1',
        showList: true,
        title:null,
        roleList:{},
        user:{
            userEntity:{
                status:1
            },
            authenticationEntity:{

            }

        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.roleList = {};
            vm.user = {userEntity:{
                    status:1
                },authenticationEntity:{

                }};

        },
        getDept: function(){
            //加载部门树
            $.get(baseURL + "sys/dept/list", function(r){
                ztree = $.fn.zTree.init($("#deptTree"), setting, r);
                var node = ztree.getNodeByParam("deptId", vm.user.deptId);
                if(node != null){
                    ztree.selectNode(node);

                    vm.user.deptName = node.name;
                }
            })
        },
        update: function (userId) {

            vm.showList = false;
            vm.title = "修改";

            vm.getUser(userId);
        },
        permissions: function () {
            var userId = getSelectedRow();
            if(userId == null){
                return ;
            }

            window.location.href=baseURL+"sys/permissions/index/"+userId;
        },
        del: function (id) {

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/franchisee/delete/"+id,
                    contentType: "application/json",
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            var url = vm.user.userId == null ? "sys/franchisee/save" : "sys/franchisee/update";
            vm.user.authenticationEntity.businesslicenseimg = '';
            if(vm.fileList1.length>0){
                vm.user.authenticationEntity.businesslicenseimg = vm.fileList1[0].url;
            }

            vm.user.authenticationEntity.legalidimg = '';
            if(vm.fileList2.length>0){
                vm.user.authenticationEntity.legalidimg = vm.fileList2[0].url;
            }
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.user),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        handleRemove1: function(file, fileList) {
            console.log(fileList);
            if(fileList.length>0){
                vm.fileList1 = [];
                for(var i=0;i<fileList.length;i++){
                    vm.fileList1.push({name: fileList[i].name,url: fileList[i].url});

                }
            }
            console.log(vm.fileList1);

        },
        handlePreview1: function(file) {
            console.log(file);
        },

        handsuccess1: function(response, file, fileList) {
            vm.fileList1.push({name: response.url,url: response.url});

        },

        handleRemove2: function(file, fileList) {
            console.log(fileList);
            if(fileList.length>0){
                vm.fileList2 = [];
                for(var i=0;i<fileList.length;i++){
                    vm.fileList2.push({name: fileList[i].name,url: fileList[i].url});

                }
            }
            console.log(vm.fileList2);

        },

        handlePreview2: function(file) {
            console.log(file);
        },
        handsuccess2: function(response, file, fileList) {
            vm.fileList2.push({name: response.url,url: response.url});

        },
        getUser: function(userId){
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/franchisee/info/"+userId,
                success: function(r){
                    vm.user.userEntity = r.user.userEntity;
                    if(r.user.authenticationEntity!=null){
                        vm.user.authenticationEntity = r.user.authenticationEntity;
                    }
                }
            });
        },
        getRoleList: function(){
            $.get(baseURL + "sys/role/select", function(r){
                vm.roleList = r.list;
            });
        },
        deptTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择部门",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级部门
                    vm.user.deptId = node[0].deptId;
                    vm.user.deptName = node[0].name;

                    layer.close(index);
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'username': vm.q.username},
                page:page
            }).trigger("reloadGrid");
        }
    }
});